﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TextEditApp.Algorithm
{
    class WordFrequency
    {
        /// <summary>
        /// 统计词频，返回的是单词和它的具体属性的哈希表(排序后）
        /// </summary>
        /// <param name="text"></param>
        /// <param name="d"></param>
        /// <returns></returns>
        public static IOrderedEnumerable<KeyValuePair< String, (int cnt, bool is_en)>> GetWordFrequency(string text, SimpleDictionary d)
        {
            Dictionary<string, (int cnt, bool is_en)> fre = new Dictionary<string, (int cnt, bool is_en)>();
            string[] words = Util.GetWordArray(text);
            foreach (string w in words)
            {
                if (w.Length == 0) continue;
                string word = w.ToLower();
                if (fre.ContainsKey(word))
                {
                    (int cnt, bool is_en) kv = fre[word];
                    kv.cnt++;
                    fre[word] = kv;
                }
                else fre.Add(word.ToLower(), (1, d.isEnglish(word)));
            }
            
            return from objMap in fre orderby objMap.Value.cnt descending select objMap; 
        }
    }
}
